package com.jonniespratley.flexdatabasemanager.factories
{
	public class SequenceEventFactory
	{
		public function SequenceEventFactory(){}
		
		public static function chainEvents( eventsArray:Array ):SequenceChainEvent
		{
			//get the length of events in the array
			var length:int = eventsArray.length;
			
			//if there is less then one, return
			if ( length < 1 )
			{
				return null;
				trace( 'Not enough events to be in sequence' );
			}
			
			//returnEvent 
			var returnEvent:SequenceChainEvent = eventsArray[ 0 ] as SequenceChainEvent;
			
			//var i:int = length - 1;
			for ( var i:int = length -1; i >= 0; i--  )
			{
				if ( i != ( length - 1 ) )
				{
					//set up the events
					var seqEvent:SequenceChainEvent = eventsArray[ i ] as SequenceChainEvent;
					var next_seqEvent:SequenceChainEvent = eventsArray[ i + 1 ] as SequenceChainEvent;
						seqEvent.nextEventSequence = next_seqEvent;
				}
			}
			return returnEvent;
		}
	}
}